package com.cn.newetv.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.cn.newetv.content.EtvData;
import com.cn.newetv.po.FavFilmDataBaseDO;
import com.cn.newetv.po.VFilmDO;

public class FavFilmDao {
	private ContentResolver contentResolver;
	public void openDB(Context context) {
		try {
			contentResolver = context.getContentResolver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(FavFilmDataBaseDO favFilmDataBaseDO, Integer mode) {
		try {
			ContentValues values = new ContentValues();
			values.put(EtvData.FavFilmDO.FFILMID, favFilmDataBaseDO.getId());
			values.put(EtvData.FavFilmDO.FINTIME, EtvUtils.getDateTime(new Date()));
			values.put(EtvData.FavFilmDO.FTYPE, favFilmDataBaseDO.getType());
			values.put(EtvData.FavFilmDO.FNAME, favFilmDataBaseDO.getName());
			values.put(EtvData.FavFilmDO.FPIC, favFilmDataBaseDO.getPic());
			values.put(EtvData.FavFilmDO.FMODE, mode);
			contentResolver.insert(EtvData.FavFilmDO.WORD_CONTENT_URI, values);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteById(String filmId, Integer mode) {
		try {
			String selection = "filmId=? and mode=?";
			String[] selectionArgs = new String[]{filmId,""+mode};
			contentResolver.delete(EtvData.FavFilmDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public FavFilmDataBaseDO getById(String filmId, Integer mode) {
		try {
			String[] projection = new String[]{EtvData.FavFilmDO.FFILMID,EtvData.FavFilmDO.FNAME,EtvData.FavFilmDO.FTYPE,EtvData.FavFilmDO.FPIC,EtvData.FavFilmDO.FINTIME};
			String selection = "filmId=? and mode=?";
			String[] selectionArgs = new String[]{filmId,""+mode};
			Cursor cursor = contentResolver.query(EtvData.FavFilmDO.WORD_CONTENT_URI, projection, selection, selectionArgs, null);
			if(cursor == null || cursor.getCount() == 0)
				return null;
			cursor.moveToNext();
			FavFilmDataBaseDO favFilmDataBaseDO = new FavFilmDataBaseDO();
			favFilmDataBaseDO.setId(cursor.getString(0));
			favFilmDataBaseDO.setName(cursor.getString(1));
			favFilmDataBaseDO.setType(cursor.getString(2));
			favFilmDataBaseDO.setPic(cursor.getString(3));
			favFilmDataBaseDO.setIntime(cursor.getString(4));
			return favFilmDataBaseDO;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<VFilmDO> findAll(Integer mode) {
		List<VFilmDO> res = new ArrayList<VFilmDO>();
		try {
			String[] projection = new String[]{EtvData.FavFilmDO.FFILMID,EtvData.FavFilmDO.FNAME,EtvData.FavFilmDO.FTYPE,EtvData.FavFilmDO.FPIC,EtvData.FavFilmDO.FINTIME};
			String sortOrder = "intime desc";
			String selection = "mode=?";
			String[] selectionArgs = new String[]{""+mode};
			Cursor cursor = contentResolver.query(EtvData.FavFilmDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				//res.put(cursor.getString(0), cursor.getString(1));
				VFilmDO data = new VFilmDO();
				data.setId(cursor.getString(0));
				data.setName(cursor.getString(1));
				data.setType(cursor.getString(2));
				data.setPic(cursor.getString(3));
				res.add(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public List<VFilmDO> findPage(int pagesize, int page, Integer mode) {
		List<VFilmDO> res = new ArrayList<VFilmDO>();
		try {
			String[] projection = new String[]{EtvData.FavFilmDO.FFILMID,EtvData.FavFilmDO.FNAME,EtvData.FavFilmDO.FTYPE,EtvData.FavFilmDO.FPIC,EtvData.FavFilmDO.FINTIME};
			String sortOrder = "intime desc";
			String limit = ""+pagesize+" offset " + (page*pagesize);
			String selection = "mode=?";
			String[] selectionArgs = new String[]{""+mode};
			//String limit = "1,0";
			Cursor cursor = contentResolver.query(EtvData.FavFilmDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder+"|"+limit);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				//res.put(cursor.getString(0), cursor.getString(1));
				VFilmDO data = new VFilmDO();
				data.setId(cursor.getString(0));
				data.setName(cursor.getString(1));
				data.setType(cursor.getString(2));
				data.setPic(cursor.getString(3));
				//favFilmDataBaseDO.setIntime(cursor.getString(4));
				res.add(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}
